package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import model.general.LoadImage;
import model.general.Location;
import model.general.Util;
import model.objects.FunnyObject;

import control.ControlGame;
import control.IControlGame;

public class Map extends JPanel implements KeyListener {
	private IControlGame control;

	private int PWIDTH = Util.DEFAULT_WIDTH_PANEL,
			PHEIGHT = Util.DEFAULT_HEIGHT_PANEL;

	private FunnyObject bubble;
	private BufferedImage gameImagePause;
	private BufferedImage gameImageStatusbar;

	private LoadImage loadImg = new LoadImage();

	// for displaying messages
	private Font msgsFont;
	private FontMetrics metrics;

	public Map() {
		setDoubleBuffered(false);
		setBackground(Color.black);
		setPreferredSize(new Dimension(PWIDTH, PHEIGHT));

		setFocusable(true);
		requestFocus(); // the JPanel now has focus, so receives key events

		control = new ControlGame(this);
		control.startGame();

		// set up message font
		msgsFont = new Font("SansSerif", Font.BOLD, 24);
		metrics = this.getFontMetrics(msgsFont);

		// ve bong bong'
		String[] arrImgBubbles = { "bubbles.gif", "bubbles-2.gif",
				"bubbles-3.gif" };
		bubble = new FunnyObject(new Location(700, 400), arrImgBubbles[0], arrImgBubbles[0],
				this, arrImgBubbles);
		bubble.setImgChange(arrImgBubbles);

		gameImagePause = loadImg.getbufferImage("game_paused.png");
		gameImageStatusbar = loadImg.getbufferImage("status_bar.png");

		addKeyListener(this);
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
	}

	@Override
	public void keyPressed(KeyEvent e) {
		move(e);
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		move(e);
	}

	public void move(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			control.moveBoatLeft();
		}
		if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			control.moveBoatRight();
		}
		if (e.getKeyCode() == KeyEvent.VK_DOWN
				|| e.getKeyCode() == KeyEvent.VK_SPACE) {
			control.throwHook();
		}
		if (e.getKeyCode() == KeyEvent.VK_P) {
			control.pauseGame();
		}
		if (e.getKeyCode() == KeyEvent.VK_R) {
			control.resumeGame();
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	public void updateAllElement() {
//		bubble.update();
	}

	public void paintSomethingFun(Graphics g) {
		bubble.show(g);
	}

	public void paintGameStartWarning(Graphics g) {
	}

	/* paint the game pause warning */
	public void paintGamePausedWarning(Graphics g) {
		paintStatusBar(g);
		paintSomethingFun(g);
		g.drawImage(gameImagePause, Util.DEFAULT_WIDTH_PANEL / 6,
				Util.DEFAULT_HEIGHT_PANEL / 3, null);
	}

	public void paintGameWonWarning(Graphics g) {
		paintStatusBar(g);
		paintSomethingFun(g);
	}

	public void paintGameLostWarning(Graphics g) {
		paintStatusBar(g);
		paintSomethingFun(g);
	}

	/* warning before playing a normal level */
	public void paintGameLevelUp(Graphics g) {
		paintStatusBar(g);
		paintSomethingFun(g);
	}

	public void paintStatusBar(Graphics g) {
		int widthStatusBar = gameImageStatusbar.getWidth();
		int heightStatusBar = gameImageStatusbar.getHeight();

		int xDraw = this.getWidth() - widthStatusBar;
		int yDraw = 0;

		// paint status bar
		g.fillRect(0, 0, this.getWidth(), heightStatusBar);
		g.drawImage(gameImageStatusbar, xDraw, yDraw, null);

	}

	public void paintGameElements(Graphics g) {

	}
	//
	// public void paintOptionOfBoat(Graphics g) {
	// int xHeart = control.getBoat().getLife();
	//
	// for (int i = 0; i < lifePlayer; i++) {
	// g.drawImage(heart, xHeart, 10, this);
	// xHeart += 15;
	// }
	//
	// g.setFont(new Font("Courier New", Font.PLAIN, 12));
	// if (totalShot != 0) {
	// hitRate = 100 * niceShot / totalShot;
	// }
	// /* bottom status bar */
	// g.setColor(Color.white);
	// g.drawString("LEVEL: " + level, 189, 20);
	// g.setColor(Color.YELLOW);
	// g.drawString("SCORE: " + score, 249, 20);
	// g.setColor(Color.black);
	// g.fillRect(0, gameHeight - 30, gameWidth, 30); // black background
	// g.setColor(Color.white);
	// g.drawString("[ + NICE SHOT: " + niceShot + "][ + MISSED SHOT: "
	// + missedShot + "][ + TOTAL SHOT: " + totalShot
	// + "][ + HIT RATE: " + hitRate + "%][made by Gapi Snake]", 80,
	// gameHeight - 10);
	// }

}
